﻿using CocoaAni.Net.WebSockets.Server.ArgAdapters.Base;
using CocoaAni.Net.WebSockets.Server.Attributes;
using CocoaAni.Net.WebSockets.Server.DefineInfos;
using CocoaAni.Net.WebSockets.Server.Exceptions;
using Newtonsoft.Json;

namespace CocoaAni.Net.WebSockets.Server.ArgAdapters;

public class JsonObjectArgAdapter : MessageArgAdapter<object, JsonArgAttribute>
{
    public override object? GetTargetParameter(MessageArgContext argContext)
    {
        try
        {
            return JsonConvert.DeserializeObject(argContext.Message.ToString(), argContext.ArgDefineInfo.Type);
        }
        catch (Exception e)
        {
            throw new WebSocketMessageArgException("JsonObject 反序列化失败！" + e.Message, e, argContext.WebSocketContext);
        }
        
    }
}